<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Registered_User extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by_value);exit;
					}
				}
			$results_per_page = 3;
		
		
		if($this->uri->segment(4) == 2){
			$data = array('www-command'=>'registerusers-get_associationusers', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			$response = $this->get_users_list($data);
			
		}elseif($this->uri->segment(4) == 3){
			$data = array('www-command'=>'registerusers-get_users', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			$response = $this->get_users_list($data);
		
		}else{
			$data = array('www-command'=>'registerusers-all_registerusers', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			$response = $this->get_users_list($data);
		}
		
		if($response->message !== 'No Review'){
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/registered_user/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['registerusers'] = $response;
			$data['error'] = '';
			}else{
				$data['error'] = 1;
				$data['all_registerusers'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['user_fields'] = array('user_name'=>'Name', 'email'=>'Email', 'city'=>'City', 'status'=>'Status', 'created_date'=>'Date');
			$this->load->view('admin/registered_user', $data);
 		}

		function get_users_list($data){
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				return $response;
			}
			
			function create_registered_user(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('password', 'Password', 'required');
				$this->form_validation->set_rules('phone', 'Phone', 'required|regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('address1', 'Address1', 'required');
				$this->form_validation->set_rules('state', 'State', 'required|callback_state');
				
				if($this->form_validation->run() == FALSE){
						
				$data = array('www-command'=>'registerusers-get_states_list');
				$get_states_list = $this->get_users_list($data);
				$data['get_states_list'] = $get_states_list;
				//echo '<pre>';print_r($get_states_list);exit;
				$this->load->view('admin/create_registered_user_page', $data);
				}else{
					if($this->input->post('is_association') == 'on'){
						$is_association = 1;
						$association_name = $this->input->post('association_name');
						$association_email = $this->input->post('association_email');
						$association_info = $this->input->post('association_info');
					}else{
						$is_association = 0;
						$association_name = '';
						$association_email = '';
						$association_info = '';
					}
					
				$filesupload = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);	
				//echo $this->input->post('event_id');exit;
				$data = array('www-command'=>'registerusers-create_registeruser', 
					'user_name' => $this->input->post('name'),
					'email' => $this->input->post('email'),
					'password' => $this->input->post('password'),
					'company' => $this->input->post('company'),
					'phone_number'=>$this->input->post('phone'),
					'fax_number'=>$this->input->post('fax'),
					'address_1'=>$this->input->post('address1'),
					'address_2'=>$this->input->post('address2'),
					'city'=>$this->input->post('city'),
					'state_id'=>$this->input->post('state'),
					'zipcode'=>$this->input->post('zip'),
					'user_image'=>$filesupload,
					'is_association'=>$is_association,
					'association_name'=>$association_name,
					'association_email'=>$association_email,
					'association_info'=>$association_info
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_users_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/registered_user/index/');
				}
			}
			
			function state($state){
			if($state == ""){
				$this->form_validation->set_message('state','Please Select state');
				return false;
			}else{
				return true;
			}
		}
		
		function search_by(){
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$this->input->post('search')
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/registered_user/index/'.$query_id);
			
		}
		
		function edit_registered_user(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('password', 'Password', 'required');
				$this->form_validation->set_rules('phone', 'Phone', 'required|regex_match[/^[0-9]{10}$/]');
				$this->form_validation->set_rules('address1', 'Address1', 'required');
				$this->form_validation->set_rules('state', 'State', 'required|callback_state');
				
				if($this->form_validation->run() == FALSE){
						if($this->uri->segment(4) > 0){
							$_SESSION['user_id'] = $this->uri->segment(4);
						}
				$data = array('www-command'=>'registerusers-get_registeruser', 'registereduser_id'=>$_SESSION['user_id']);
				//echo '<pre>';print_r($data);exit;
				$get_registeruser = $this->get_users_list($data);
				
				$data = array('www-command'=>'registerusers-get_states_list');
				$get_states_list = $this->get_users_list($data);
				$data['get_registeruser'] = $get_registeruser;
				$data['get_states_list'] = $get_states_list;
				//echo '<pre>';print_r($get_registeruser);exit;
				$this->load->view('admin/edit_registered_user_page', $data);
				}else{
					if($this->input->post('is_association') == 'on'){
						$is_association = 1;
						$association_name = $this->input->post('association_name');
						$association_email = $this->input->post('association_email');
						$association_info = $this->input->post('association_info');
					}else{
						$is_association = 0;
						$association_name = '';
						$association_email = '';
						$association_info = '';
					}
					
				$filesupload = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);	
				//echo $this->input->post('event_id');exit;
				$data = array('www-command'=>'registerusers-edit_registeruser', 
					'user_name' => $this->input->post('name'),
					'email' => $this->input->post('email'),
					'password' => $this->input->post('password'),
					'company' => $this->input->post('company'),
					'phone_number'=>$this->input->post('phone'),
					'fax_number'=>$this->input->post('fax'),
					'address_1'=>$this->input->post('address1'),
					'address_2'=>$this->input->post('address2'),
					'city'=>$this->input->post('city'),
					'state_id'=>$this->input->post('state'),
					'zipcode'=>$this->input->post('zip'),
					'user_image'=>$filesupload,
					'registereduser_id'=>$_SESSION['user_id'],
					'is_association'=>$is_association,
					'association_name'=>$association_name,
					'association_email'=>$association_email,
					'association_info'=>$association_info
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_users_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/registered_user/index/');
				}
			}
			
			function delete_registered_user(){
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'registerusers-delete_registeruser','status'=>$status, 'registereduser_id'=>$this->uri->segment(4));
				$response = $this->get_users_list($data);
				//echo '<pre>';print_r($response);exit;
				redirect('admin/registered_user/index/');
			}
			
}